package com.mozz.finally_work.servlet.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mozz.finally_work.dao.TagDao;
import com.mozz.finally_work.pojo.Tag;
import com.mozz.finally_work.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "QueryAllTagServlet", value = "/QueryAllTagServlet")
public class QueryAllTagServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*获取所有的标签*/
        SqlSession session = MybatisUtils.getSession();
        TagDao dao = session.getMapper(TagDao.class);
        List<Tag> tags = dao.queryAllTagName();
        /*转为json对象*/
        session.close();
        ObjectMapper mapper = new ObjectMapper();
        String result = mapper.writeValueAsString(tags);
        response.setHeader("Content-Type", "application/json; charset=UTF-8");
        response.getWriter().write(result);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
